import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
/**
 * 假设ArrayList中存储的元素是整型数字1~5，遍历每个元素，将每个元素顺序输出。要求：使用多种方式实现。
 */
public class Test1 {
    public static void main(String[] args) {

        ArrayList<Integer> numbers = new ArrayList<>();
        Collections.addAll(numbers, 1, 2, 3, 4, 5);

        System.out.println("方式1：for循环（索引方式）");
        for (int i = 0; i < numbers.size(); i++) {
            System.out.print(numbers.get(i) + " ");
        }
        System.out.println("\n");

        System.out.println("方式2：增强for循环（foreach）");
        for (int num : numbers) {
            System.out.print(num + " ");
        }
        System.out.println("\n");

        System.out.println("方式3：迭代器（Iterator）");
        Iterator<Integer> iterator = numbers.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println("\n");

        System.out.println("方式4：Java 8+ forEach()方法（Lambda表达式）");
        numbers.forEach(num -> System.out.print(num + " "));
        System.out.println("\n");

        System.out.println("方式5：Java 8+ 流（Stream）");
        numbers.stream().forEach(num -> System.out.print(num + " "));
        System.out.println();

//        numbers.forEach(System.out::print);
    }
}